package com.htsk.city.services.core.dao;

import com.htsk.city.services.core.domain.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * (Menu)表数据库访问层
 *
 * @author makejava
 * @since 2024-09-25 18:13:07
 */
public interface MenuDao extends JpaRepository<Menu,Integer> {

    @Query(value = "SELECT \n" +
            "    m.* \n" +
            "FROM menu m \n" +
            "WHERE \n" +
            "    EXISTS (\n" +
            "        SELECT 1 \n" +
            "        FROM role r \n" +
            "        JOIN sys_user_role ur ON r.id = ur.role_id \n" +
            "        WHERE r.id = 1 \n" +
            "        AND ur.user_id = ?1\n" +
            "    )\n" +
            "    OR EXISTS (\n" +
            "        SELECT 1 \n" +
            "        FROM role r \n" +
            "        JOIN sys_user_role ur ON r.id = ur.role_id \n" +
            "        JOIN role_menu rm ON r.id = rm.role_id \n" +
            "        WHERE ur.user_id = ?1\n" +
            "        AND rm.menu_id = m.id\n" +
            "    )\n" +
            "ORDER BY m.parent_id, m.menu_sort", nativeQuery = true)
    List<Menu> queryByUser(Integer userId);

}

